package com.luoy.util;

import com.luoy.entity.CutDate;
import com.luoy.enums.DateEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @desc:  大时间段切分为小时间片
 * @author: luoy
 * @date: 2020-12-09 19:16
 */

@Component
@Slf4j
public class DateCalculate {

    private static String[] parsePatterns = {"yyyy-MM-dd","yyyy年MM月dd日",
            "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
            "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd"};

//
    @Value("${split.num}")
    private int splitMax;

    @Value("${split.day}")
    private int splitDay;
    /**
     * 切割时间段
     * @Author: luoy
     * @param dateType 交易类型 M/D/H/N -->每月/每天/每小时/每分钟
     * @param start yyyy-MM-dd HH:mm:ss
     * @param end  yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static List<CutDate> cutDate(String dateType, String start, String end) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dBegin = sdf.parse(start);
            Date dEnd = sdf.parse(end);
            return findDates(dateType, dBegin, dEnd);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static List<CutDate> findDates(String dateType, Date dBegin, Date dEnd) throws Exception {
        List<CutDate> listDate = new ArrayList<>();
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calBegin.getTime());
        while (calEnd.after(calBegin)) {
            switch (EnumUtils.getEnum(DateEnum.class,dateType)) {
                case MONTH:
                    calBegin.add(Calendar.MONTH, 1);
                    break;
                case DAY:
                    calBegin.add(Calendar.DAY_OF_YEAR, 1);
                    break;
                case HOUR:
                    calBegin.add(Calendar.HOUR, 1);
                    break;
                case MINUTE:
                    calBegin.add(Calendar.MINUTE, 1);
                    break;
                case SECOND:
                    calBegin.add(Calendar.SECOND, 1);
                    break;

                default:
                    return null;
            }
            if (calEnd.after(calBegin)){
                CutDate build = CutDate.builder()
                        .startTime(startTime)
                        .endTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calBegin.getTime()))
                        .build();
                listDate.add(build);
                startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calBegin.getTime());
            }

            else{
                CutDate build = CutDate.builder()
                        .startTime(startTime)
                        .endTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calEnd.getTime()))
                        .build();
                listDate.add(build);
                startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calEnd.getTime());
            }

        }
        return listDate;
    }


    public static void main33(String[] args) {
        String start = "2016-03-01 12:56:03";
        String end = "2016-03-01 13:12:07";
        List<CutDate> list = cutDate(DateEnum.MINUTE.name(), start, end);
        for (CutDate str :list){
            System.out.println(str);
        }
    }

    public static void main11(String[] args) {
        String start = "2016-03-01 12:56:03";
        String end = "2016-03-01 13:12:07";
        String timeType = getTimeType(start, end, 5000);
        List<CutDate> list = cutDate(timeType, start, end);
        for (CutDate str :list){
            System.out.println(str);
        }
    }

    public static void main(String[] args) {

    }


    public static void main3(String[] args) {
        Date dd = parseDate("2016-03-01 12:56:03");
        System.out.println(dd);
    }


    /**
     * @desc:  判断字符串是否时间类型
     * @Author: luoy
     * @param str :
     * @Date: 2020/12/10 10:02 上午
     */
    public static Date parseDate(String str) {
        if(StringUtils.isEmpty(str)) {
            return null;
        }
        try {
            return DateUtils.parseDate(str, parsePatterns);
        } catch (java.text.ParseException e) {
            return null;
        }
    }


    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //不同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //同一年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }


    public static int differentDays(String date1,String date2)
    {
        Calendar cal1 = Calendar.getInstance();

        cal1.setTime(strToDate(date1));

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(strToDate(date2));
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //不同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

           return timeDistance + (day2-day1) ;
        }
        else    //同一年
        {
           log.info("判断day2 - day1 : " + (day2-day1));
            return   day2-day1;
        }
    }
    public static void main6(String[] args) {
        String start = "2016-03-01 12:56:03";
        String end = "2016-03-03 12:56:03";
        int i = differentDays(start, end);
        System.out.println(i);
    }

    public static Date strToDate(String str){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(str);
            System.out.println(date);
            return date;
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }


    public static String getTimeType(String start,String end,long num){
        int i = differentDays(start, end);
        if (i<10 && num >5000000){
            return DateEnum.MINUTE.name();
        }else {
            return  DateEnum.DAY.name();
        }
    }
}
